<!--
 * @Description: 限时秒杀活动管理列表
 * @Author: liyujie
 * @Date: 2021/08/18
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/18
-->
<template>
    <PageLayout title="活动列表">
        <template slot="header-right">
            <el-button
                size="medium"
                type="primary"
                icon="el-icon-setting"
                @click="handleRoute('seckillActivityConfig')"
            >
                活动设置
            </el-button>
            <el-button
                size="medium"
                type="primary"
                icon="el-icon-plus"
                @click="handleRoute('seckillActivityEdit', {type: 'add'})"
            >
                创建活动
            </el-button>
        </template>
        <!-- 筛选-->
        <template slot="filter">
            <div class="filter-item">
                <div class="filter-item__title">
                    活动信息
                </div>
                <el-input
                    placeholder="搜索活动编号，活动名称"
                    v-model="filterData.keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    style="width: 260px"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    报名进度
                </div>
                <el-select
                    v-model="filterData.status"
                    size="medium"
                    style="width: 260px"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <el-button
                    type="primary"
                    size="small"
                    class="query-button"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>
            </div>
        </template>
        <!--筛选-->
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            />
            <el-table-column
                prop="activity_no"
                label="活动编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="title"
                label="活动名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="商家报名时间"
                align="left"
                :formatter="emptyFormatter"
                min-width="180"
            >
                <template slot-scope="scope">
                    {{ scope.row.apply_start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }} -
                    {{ scope.row.apply_end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="活动进度"
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.activity_status === 0 ? 'warning' : (scope.row.activity_status === 1 ? 'success' : 'normal')]"/>
                        {{ scope.row.activity_status | statusFilter }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="商家端状态"
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.status ? 'success' : 'normal']"/>
                        {{ scope.row.status ? '已上架' : '已下架' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="total_goods_count"
                label="报名商品数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="wait_verify_goods_count"
                label="待审核商品数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                label="操作"
                align="left"
                width="270"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('seckillGoodsVerify', {activity_no: scope.row.activity_no, title: scope.row.title})"
                        >
                            审核商品
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleSetStatus(scope.row)"
                        >
                            {{ scope.row.status ? '下架' : '上架' }}
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleEdit(scope.row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('seckillActivityEdit', {activity_no: scope.row.activity_no, type: 'copy'})"
                        >
                            复制
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            :style="{color: scope.row.status === 1 ? '#C0C4CC' : '#F95B56'}"
                            :disabled="scope.row.status === 1"
                            @click="handleDelete(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import {back} from '@/mixin/back';

export default {
    name: 'SeckillActivityList',
    mixins: [back],
    components: {},
    data() {
        return {
            filterData: {
                // 活动信息
                keyword: '',
                status: 99,
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: 99},
                {label: '报名未开始', value: 0},
                {label: '报名进行中', value: 1},
                {label: '报名已结束', value: 2},
            ],
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '报名未开始';
                case 1:
                    return '报名进行中';
                case 2:
                    return '报名已结束';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'},{title: '活动管理'},{title: '限时秒杀'}, {title: '活动列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {

            this.tableLoading = true;
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_goods_flash_sale_activity%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 编辑
         * @param row
         * @returns {boolean}
         */
        handleEdit(row) {
            if (!this.authResult.auth_edit) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.handleRoute('seckillActivityEdit', {activity_no: row.activity_no, type: 'edit'});
        },

        /**
         * 删除
         * @param row
         * @returns {boolean}
         */
        handleDelete(row) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$zdConfirm('删除后，该活动报名记录将会被清空，包含商家端的数据，确认是否要删除？', '温馨提示', {
                confirmButtonText: '确认删除',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_goods_flash_sale_activity%5Cdelete', {activity_no: row.activity_no}, res => {
                    if (res.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: res.msg,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 上下架
         * @param row
         * @returns {boolean}
         */
        handleSetStatus(row) {
            if (!this.authResult.auth_set_status) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$post('/student/shop_goods_flash_sale_activity%5Cset_status', {
                activity_no: row.activity_no,
                status: row.status ? 0 : 1
            }, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },
    }
};
</script>

<style lang="less" scoped>
</style>
